<?php

function reequipment_userapi_getschedule($args)
{
    extract($args);

    // Optional arguments
    if (!isset($startnum)) {$startnum = 1;}
    if (!isset($numitems)) {$numitems = -1;}
    
    // Need to allow OR in the where clause.  Probably a more elegant way to do this.  Think about it...
    if (!isset($or)){ 
    	$or = ' AND ';
    } else {
    	$or = ' OR ';
    }

    $dbconn =& xarDBGetConn();
    $xartable =& xarDBGetTables();
    $table = $xartable['rescheduled'];
    $join_area = $xartable['reareas'];
    $join_model = $xartable['remodels'];
    $join_frequency = $xartable['refrequency'];
    $join_maintenance = $xartable['remaintenance'];

    $query = "SELECT s.schedule_id, 
    				 s.area_id, 
    				 a.area_name, 
    				 s.model_id, 
    				 m.model_name, 
    				 m.current, 
    				 s.frequency_id, 
    				 f.frequency, 
    				 s.issue, 
    				 s.item_ids, 
    				 s.manhours, 
    				 s.downtime"
    		. " FROM $table s"
    		
    		. " LEFT OUTER JOIN $join_area a"
    		. " ON s.area_id = a.area_id"
    		
    		. " LEFT OUTER JOIN $join_frequency f"
    		. " ON s.frequency_id = f.frequency_id"  
    		
    		. " LEFT OUTER JOIN $join_model m"
    		. " ON s.model_id = m.model_id"; 
	
    $where = array();
    $bind = array();
    
    if (!empty($schedule_id)) {
        $where[] = 's.schedule_id = ?';
        $bind[] = (int)$schedule_id;
    }

    if (!empty($model_id)) {
        $where[] = 's.model_id = ?';
        $bind[] = (int)$model_id;
    }
    
    if (!empty($area_id)) {
        $where[] = 's.area_id = ?';
        $bind[] = (int)$area_id;
    }

    if (!empty($frequency)) {
        $where[] = 's.frequency = ?';
        $bind[] = (int)$frequency;
    }
    
    if (!empty($manhours)) {
        $where[] = 's.manhours = ?';
        $bind[] = (int)$manhours;
    }    
    
    if (!empty($downtime)) {
        $where[] = 's.downtime = ?';
        $bind[] = (int)$downtime;
    }    

    if (!empty($where)) {
        $query .= ' WHERE ' . implode($or, $where);
    }

    // Not really necessary
    $query .= " ORDER BY s.frequency_id";
    
    
    // return var_export($query);

    $result =& $dbconn->SelectLimit($query, $numitems, $startnum-1, $bind);
    if (!$result) return;

    $tags = array();
    for (; !$result->EOF; $result->MoveNext()) {
        list($schedule_id, $area_id, $area_name, $model_id, $model_name, $current, $frequency_id, $frequency, $issue, $item_ids, $manhours, $downtime) = $result->fields;

        $tags[] = array(
        		'schedule_id'	=> $schedule_id,
        		'area_id'    	=> $area_id,
                'area_name'		=> xarVarPrepForDisplay($area_name),
                'model_id'      => $model_id,
                'model_name'    => xarVarPrepForDisplay($model_name),
                'current'		=> $current,
                'frequency_id' 	=> $frequency_id,
                'frequency'    	=> xarVarPrepForDisplay($frequency),
                'issue'			=> xarVarPrepHTMLDisplay($issue),
                'item_ids'		=> $item_ids,
                'manhours'		=> $manhours,
                'downtime'		=> $downtime
        );
    }

    $result->Close();
    return $tags;
}
?>